//
//  RentListViewController.m
//  ZhuanZuBao
//
//  Created by bchy on 2020/8/28.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "SpecialViewController.h"
#import "RentCell.h"
#import "RentDetailViewController.h"
#import "RentModel.h"
#import "ChooseAreaView.h"
#import "ChooseMoneyView.h"
#import "ChooseRoomTypeView.h"
#import "SearchViewController.h"

@interface SpecialViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;


//@property (nonatomic, assign) NSInteger type;

@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;

@property (weak, nonatomic) IBOutlet UIButton *cityBtn;
@property (weak, nonatomic) IBOutlet UIButton *moneyBtn;
@property (weak, nonatomic) IBOutlet UIButton *roomTypeBtn;

@property (nonatomic, strong) ChooseAreaView *areaView;
@property (nonatomic, copy) NSString *district;

@property (nonatomic, strong) ChooseMoneyView *moneyView;
@property (nonatomic, assign) NSInteger minMoney;
@property (nonatomic, assign) NSInteger maxMoney;

@property (nonatomic, strong) ChooseRoomTypeView *roomTypeView;
@property (nonatomic, copy) NSString *roomType;
@property (nonatomic, copy) NSString *keyword; //搜索关键字

@property (nonatomic, copy) NSString *city;

@end

@implementation SpecialViewController



-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (BOOL)fd_prefersNavigationBarHidden{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (![self.city isEqualToString:[User sharedUser].currentCity]) {
        self.city = [User sharedUser].currentCity;
        self.areaView.city = self.city;
        self.district = @"";
        [self.cityBtn setTitle:[NSString stringWithFormat:@"全%@ ", _city] forState:UIControlStateNormal];
        [self getData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _city = @"南京";
    _district = @"";
    _roomType = @"";
    _keyword = @"";
    self.title = @"特价";
    
    [self setupUI];
    
    
    _page = 1;
    [self getData];
    [MBProgressHUD showMessag:@"" toView:self.view];
}

-(void)setupUI{
    [_navView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(SafeAreaTopHeight);
    }];
    self.view.backgroundColor = [UIColor whiteColor];
    _searchBtn.layer.cornerRadius = 18;
    _searchBtn.layer.masksToBounds = YES;
    
    self.tableView.separatorColor = SEPARATECOLOR;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        [self getData];
    }];
    __weak typeof(self) weakSelf = self;
    _areaView = [ChooseAreaView new];
    _areaView.hidden = YES;
    _areaView.city = _city;
    _areaView.selectAreaBlock = ^(NSString * _Nonnull district) {
        weakSelf.district = district;
        if (district && district.length > 0) {
            [weakSelf.cityBtn setTitle:[NSString stringWithFormat:@"%@ ", district] forState:UIControlStateNormal];
        }else{
            [weakSelf.cityBtn setTitle:[NSString stringWithFormat:@"全 %@", weakSelf.city] forState:UIControlStateNormal];
        }
        [weakSelf.cityBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.cityBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    _areaView.cancelBlock = ^{
        [weakSelf.cityBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.cityBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    };
    [self.view addSubview:_areaView];
    [_areaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(self.cityBtn.mas_bottom);
    }];
    [self.cityBtn setTitle:[NSString stringWithFormat:@"全%@ ", _city] forState:UIControlStateNormal];
    
    _moneyView = [ChooseMoneyView new];
    _moneyView.hidden = YES;
    _moneyView.selectMoneyBlock = ^(NSInteger minMoney, NSInteger maxMoney) {
        [weakSelf.moneyBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.moneyBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        weakSelf.minMoney = minMoney;
        weakSelf.maxMoney = maxMoney;
        if (maxMoney == 0) {
            [weakSelf.moneyBtn setTitle:@"不限 " forState:UIControlStateNormal];
        }else if (maxMoney == 500){
            [weakSelf.moneyBtn setTitle:@"500元以下 " forState:UIControlStateNormal];
        }else if (minMoney == 4500){
            [weakSelf.moneyBtn setTitle:@"4500以上 " forState:UIControlStateNormal];
        }else{
            [weakSelf.moneyBtn setTitle:[NSString stringWithFormat:@"%ld-%ld元 ", weakSelf.minMoney, weakSelf.maxMoney] forState:UIControlStateNormal];
        }
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    _moneyView.cancelBlock = ^{
        [weakSelf.moneyBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.moneyBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    };
    [self.view addSubview:_moneyView];
    [_moneyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(self.cityBtn.mas_bottom);
    }];
    
    _roomTypeView = [ChooseRoomTypeView new];
    _roomTypeView.hidden = YES;
    _roomTypeView.selectRoomBlock = ^(NSString * _Nonnull roomType) {
        
        [weakSelf.roomTypeBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.roomTypeBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
        if ([roomType isEqualToString:@"不限"]) {
            weakSelf.roomType  = @"";
        }else{
            weakSelf.roomType  = roomType;
        }
        
        [weakSelf.roomTypeBtn setTitle:[NSString stringWithFormat:@"%@ ", roomType] forState:UIControlStateNormal];
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    _roomTypeView.cancelBlock = ^{
        [weakSelf.roomTypeBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [weakSelf.roomTypeBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    };
    [self.view addSubview:_roomTypeView];
    [_roomTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.top.equalTo(self.cityBtn.mas_bottom);
    }];
    
    _cityBtn.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _cityBtn.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _cityBtn.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    
    _moneyBtn.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _moneyBtn.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _moneyBtn.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    
    _roomTypeBtn.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _roomTypeBtn.titleLabel.transform = CGAffineTransformMakeScale(-1.0, 1.0);
    _roomTypeBtn.imageView.transform = CGAffineTransformMakeScale(-1.0, 1.0);
}


//city     否     string     城市
//adr     否     string     区县
//xq     否     string     小区
//tags     否     string     标签
-(void)getData{
    NSDictionary *dict = @{
        @"pge":@(_page),
        @"pid": @(23),
        @"adr": _district,
        @"city": _city,
        @"price_min": @(_minMoney),
        @"price_max": @(_maxMoney),
        @"hx": _roomType,
        @"cnt":@"10",
        @"key": _keyword
    };
    
    NSLog(@"房源参数：%@", dict);
    [NetWork sendGetUrl:@"fy/fy_lst.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@", responseBody);
        NSArray *arr = [RentModel mj_objectArrayWithKeyValuesArray:responseBody[@"lst"]];
        
        if (self.page == 1) {
            [self.dataArr removeAllObjects];
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer resetNoMoreData];
        }else{
            [self.tableView.mj_header endRefreshing];
            if (arr.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }else{
                [self.tableView.mj_footer endRefreshing];
            }
        }
        
        [self.dataArr addObjectsFromArray:arr];
        [self.tableView reloadData];
        if (self.dataArr.count == 0) {
            [self.tableView addSubview:self.emptyView];
        }else{
            [self.emptyView removeFromSuperview];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RentModel *rent = _dataArr[indexPath.row];
    RentCell *cell = [RentCell cellWithTableView:tableView];
    cell.rent = rent;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([User isGuest]) {
        LoginViewController *vc = [LoginViewController new];
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:vc];
        nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
        [self presentViewController:nav animated:YES completion:nil];
        return;
    }
    RentModel *rent = _dataArr[indexPath.row];
    RentDetailViewController *vc = [[RentDetailViewController alloc] initWithRentId:rent.rentId];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)search:(id)sender {
    SearchViewController *vc = [[SearchViewController alloc] initWithKeyword:_keyword];
    vc.searchWithKeyword = ^(NSString * _Nonnull keyword) {
        self.keyword = keyword;
        [self.tableView.mj_header beginRefreshing];
        [self.searchBtn setTitle:[NSString stringWithFormat:@" %@", keyword] forState:UIControlStateNormal];
    };
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)chooseCity:(id)sender {
    _areaView.hidden = !_areaView.hidden;
    _roomTypeView.hidden = YES;
    _moneyView.hidden = YES;
    if (_areaView.hidden) {
        [_cityBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [_cityBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    }else{
        [_cityBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        [_cityBtn setImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
    }
    [_moneyBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_moneyBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    
    [_roomTypeBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_roomTypeBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
}
- (IBAction)chooseMoney:(id)sender {
    _areaView.hidden = YES;
    _roomTypeView.hidden = YES;
    _moneyView.hidden = !_moneyView.hidden;
    if (_moneyView.hidden) {
        [_moneyBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [_moneyBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    }else{
        [_moneyBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        [_moneyBtn setImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
    }
    [_cityBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_cityBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    
    [_roomTypeBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_roomTypeBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
}

- (IBAction)chooseRoomType:(id)sender {
    _areaView.hidden = YES;
    _moneyView.hidden = YES;
    _roomTypeView.hidden = !_roomTypeView.hidden;
    if (_roomTypeView.hidden) {
        [_roomTypeBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
        [_roomTypeBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    }else{
        [_roomTypeBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        [_roomTypeBtn setImage:[UIImage imageNamed:@"arrowUp"] forState:UIControlStateNormal];
    }
    [_cityBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_cityBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
    
    [_moneyBtn setTitleColor:[UIColor colorWithHexString:@"#666666" andAlpha:1] forState:UIControlStateNormal];
    [_moneyBtn setImage:[UIImage imageNamed:@"arrowDown"] forState:UIControlStateNormal];
}

@end
